/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_COLLECTIONS_DYN_VECTOR_HPP
#define	_CUBEFS_CORE_API_COLLECTIONS_DYN_VECTOR_HPP

#include <cubefs/api/models/dyn_vector.hpp>
#include <cubefs/core/api/reflection/array.hpp>

namespace cubefs    {
namespace api       {

template <typename Interface>
class DataImplT: public ArrayImplT<Byte, Interface> {
	typedef DataImplT<Interface>			Me;
	typedef ArrayImplT<Byte, Interface>		Base;

public:
	virtual Int GetByteSize() const {
		return Base::size();
	}

	static void Init() {
		AbstractType *type = new AbstractType(TypeNameFactory<Interface>::name(), ByteArray::MyType());
		type_ = type;

		type->AddOperation(new BasicOperation<Int (Me::*)(), Int (Me::*)() const>(type, "GetByteSize", &Me::GetByteSize));
	}

public:
    CUBEFS_DECLARE_REFLECTION;
};

template <typename Interface>
Type* DataImplT<Interface>::type_			= NULL;
typedef DataImplT<Data>						DataImpl;

CUBEFS_DEFINE_PARAMETER_TYPE(Data);



template <typename Interface>
class DataIteratorImplT: public IteratorImplT<Interface> {
	typedef DataIteratorImplT<Interface>	Me;
	typedef IteratorImplT<Interface>		Base;
public:

    virtual BigInt Read(Data* data, BigInt start, BigInt len)       = 0;
    virtual void Insert(Data* data, BigInt start, BigInt len)       = 0;
    virtual void Update(Data* data, BigInt start, BigInt len)       = 0;
    virtual void Remove(BigInt len)                                 = 0;
    virtual BigInt Skip(BigInt distance)                            = 0;

    static void Init() {
        AbstractType *type = new AbstractType(TypeNameFactory<Interface>::name(), Iterator::MyType());
        type_ = type;

        type->AddOperation(new BasicOperation<BigInt (Me::*)(Data*, BigInt, BigInt)>(type, "Read",   "data", "start", "length", &Me::Read));
        type->AddOperation(new BasicOperation<void   (Me::*)(Data*, BigInt, BigInt)>(type, "Insert", "data", "start", "length", &Me::Insert));
        type->AddOperation(new BasicOperation<void   (Me::*)(Data*, BigInt, BigInt)>(type, "Update", "data", "start", "length", &Me::Update));

        type->AddOperation(new BasicOperation<void   (Me::*)(BigInt)>(type, "Remove", "length", &Me::Remove));
        type->AddOperation(new BasicOperation<BigInt (Me::*)(BigInt)>(type, "Skip", "distance", &Me::Skip));
    }

    CUBEFS_DECLARE_REFLECTION;
};

template <typename Interface>
Type* DataIteratorImplT<Interface>::type_				= NULL;
typedef DataIteratorImplT<DataIterator>					DataIteratorImpl;

CUBEFS_DEFINE_PARAMETER_TYPE(DataIterator);



template <typename Interface>
class DynVectorImplT: public ModelImplT<Interface> {
	typedef DynVectorImplT<Interface>	Me;
	typedef ModelImplT<Interface>		Base;

public:
    virtual DataIterator *Seek(BigInt pos)                          = 0;

    static void Init() {
        AbstractType *type = new AbstractType(TypeNameFactory<Interface>::name(), Model::MyType());
        type_ = type;

        type->AddOperation(new BasicOperation<Typed* (Me::*)(BigInt), DataIterator* (Me::*)(BigInt)>(type, DataIteratorImpl::MyType(), "Seek", "pos", &Me::Seek));
    }

    CUBEFS_DECLARE_REFLECTION;
};

template <typename Interface>
Type* DynVectorImplT<Interface>::type_		= NULL;
typedef DynVectorImplT<DynVector>			DynVectorImpl;




}
}

#endif
